package com.proton.carepatchtemp.database;

import android.util.ArrayMap;

import com.proton.carepatchtemp.constant.Config;
import com.proton.carepatchtemp.constant.Extras;
import com.proton.carepatchtemp.database.table.Device;
import com.proton.carepatchtemp.database.table.Profile;
import com.proton.carepatchtemp.database.table.Report;
import com.proton.carepatchtemp.database.table.ShareHistory;
import com.proton.carepatchtemp.utils.SpUtils;

import java.util.ArrayList;
import java.util.List;

import static com.proton.carepatchtemp.constant.Config.WARNING_DIV_DEFAULT;

/**
 * Created by MoonlightSW on 2017/1/12.
 */

public class TestManager {
    public static final ArrayMap<String, RealData> container = new ArrayMap<>();

    public static RealData get(String key) {
        if (container.get(key) != null) {
            return container.get(key);
        } else {
            return new RealData();
        }
    }

    public static void put(String key, RealData value) {
        container.put(key, value);
    }

    public static void remove(String key) {
        container.remove(key);
    }

    public static Long getStartTime(String mac) {
        return null == get(mac) ? null : get(mac).startTime;
    }

    public static void setStartTime(String mac, Long startTime) {
        if (null != get(mac)) {
            get(mac).startTime = startTime;
        }
    }

    public static Long getTestTime(String mac) {
        return null == get(mac) ? 0L : get(mac).getTestTime();
    }


    public static void setFirstHisTemps(String mac, List<Float> hisTemps) {
        if (null != get(mac))
            get(mac).firstHistory.addAll(hisTemps);
    }

    public static void handleCacheTemps(String mac, List<Float> cache) {
        if (null != get(mac)) {
            get(mac).handleCacheTemps(cache);
        }
    }

    public static void patchCacheTemps(String mac, List<Float> cache) {
        if (null != get(mac)) {
            get(mac).patchCacheTemps(cache);
        }
    }

    public static void patchTempData(String mac) {
        if (null != get(mac)) {
            get(mac).patchTempData();
        }
    }

    //获取实时温度
    public static Float getRealTemp(String mac) {
        //判空
        return null == get(mac) ? null : get(mac).realTemp;
    }

    public static Profile getProfile(String mac) {
        return null == get(mac) ? null : get(mac).getProfile();
    }

    public static Long getProfileID(String mac) {
        return null == get(mac) ? null : get(mac).getProfileID();
    }

    public static void setDevice(String mac, Device device) {
        if (null != get(mac)) {
            get(mac).setDevice(device);
        }
    }

    public static Device getDevice(String mac) {
        return null == get(mac) ? null : get(mac).getDevice();
    }

    public static Long getDeviceID(String mac) {
        return null == get(mac) ? null : get(mac).getDeviceID();
    }

    public static void setReport(String mac, Report report) {
        if (null != get(mac))
            get(mac).setReport(report);
    }

    public static Long getReportID(String mac) {
        return null == get(mac) ? null : get(mac).getReportID();
    }

    public static void setFirstConnect(String mac, boolean is) {
        if (null != get(mac))
            get(mac).isFirstConnnect = is;
    }

    public static boolean isFirstConnect(String mac) {
        return null != get(mac) && get(mac).isFirstConnnect;
    }


    public static boolean isDrawRealLine(String mac) {
        return null != get(mac) && get(mac).drawRealLine;
    }

    public static void setTesting(String mac, boolean testing) {
        if (null != get(mac))
            get(mac).isTesting = testing;
    }

    public static void setDrawRealLine(String mac, boolean drawRealLine) {
        if (null != get(mac))
            get(mac).drawRealLine = drawRealLine;
    }

    public static Float getHighestTemp(String mac) {
        return null == get(mac) ? 0F : get(mac).highestTemp;
    }

    public static Float getLowestTemp(String mac) {
        return null == get(mac) ? 0F : get(mac).lowestTemp;
    }

    public static List getAllTemps(String mac) {
        return null == get(mac) ? new ArrayList<Float>() : get(mac).allTemps;
    }

    public static List<Float> getAfterTemps(String mac) {
        return null == get(mac) ? new ArrayList<Float>() : get(mac).afterDealTemps;
    }

    public static void upLoadTempData(String mac) {
        if (null != get(mac)) {
            get(mac).upLoadTempData();
        }
    }

    public static List<ShareHistory.SharelistBean> getShareList(String mac) {
        return null != get(mac) ? get(mac).shareList : null;
    }

    public static void setShareList(String mac, List<ShareHistory.SharelistBean> shareList) {
        if (null != get(mac)) {
            get(mac).shareList.clear();
            get(mac).shareList.addAll(shareList);
        }
    }

    /**
     * @return 是否正在测量
     */
    public static boolean isTesting(String mac) {
        return null != get(mac) && get(mac).isTesting;
    }

    public static void cancelShare(String mac) {

    }

    /**
     * @param mac  设备mac地址
     * @param type 类型
     * @param time 时间戳
     */
    public static boolean addNotePoint(String mac, int type, long time) {
        if (null != get(mac)) {
            get(mac).addNotePoint(type, time);
            return true;
        }
        return false;
    }

    /**
     * @return 高温报警是否开启
     */
    public static boolean isHighAlarmOpen() {
        return SpUtils.getBoolean(Extras.SETTING_HIGH_TEMP_WARNING, Config.HIGH_TEMP_WARNING_DEFAULT);
    }

    /**
     * @return 低温报警是否开启
     */
    public static boolean isLowAlarmOpen() {
        return SpUtils.getBoolean(Extras.SETTING_LOW_TEMP_WARNING, Config.LOW_TEMP_WARNING_DEFAULT);
    }

    /**
     * @return 获取报警时间间隔
     */
    public static long getAlarmInterval() {
        return SpUtils.getLong(Extras.WARNING_TIME_DIV, WARNING_DIV_DEFAULT);
    }

}
